package com.jcommander.gui.layouts;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;

public class ButtonAtRightFlowLayout extends FlowLayout {
	
	private static final long serialVersionUID = 1L;
	private final int initHeight;
	
	public ButtonAtRightFlowLayout(int align, int initHeight) {
		super(align);
		this.initHeight = initHeight;
	}
	@Override
	public Dimension preferredLayoutSize(Container target) {
		int height = initHeight;
		for (Component c : target.getComponents()) {
			if (c.isVisible()) {
				if ((c.getY() + c.getPreferredSize().height) > height) {
					height = c.getY();
					height += c.getPreferredSize().height;
				}
			}
		}
		height += ((FlowLayout)target.getLayout()).getHgap();
		return new Dimension(target.getWidth(), height);
	}
	
	@Override
	public void layoutContainer(Container target) {
		super.layoutContainer(target);
		Component lastOne = target.getComponent(target.getComponentCount()-1);
		lastOne.setLocation(target.getWidth() - lastOne.getWidth(), lastOne.getY());
	}
}
